package com.leyou.trade.web;

import com.leyou.trade.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName : PayController  //类名
 * @Description :   //描述
 * @Author : qingfeng  //作者
 * @Date: 2020-09-27 18:20  //时间
 */
@RestController
@RequestMapping("pay")
@Slf4j
public class PayController {
    private final OrderService orderService;

    public PayController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 根据订单编号创建支付链接
     * @param orderId 订单编号
     * @return 支付链接
     */
    @GetMapping("/url/{id}")
    public ResponseEntity<String> getPayUrl(@PathVariable("id") Long orderId) {
        return ResponseEntity.ok(orderService.getPayUrl(orderId));
    }

    @PostMapping(value = "/wx/notify",produces = "application/xml")
    public Map<String,String> hello(@RequestBody Map<String,String> data) throws Exception {
        /**
         * @Description //TODO 微信支付成功回调
         * @Param [data]
         * @return java.util.Map<java.lang.String, java.lang.String>
         */
        //回调处理
        log.info("[支付回调]接收微信支付回调，结果{}",data);
        orderService.handleNotify(data);
        //返回成功
        HashMap<String, String> msg = new HashMap<>();
        msg.put("return_code","SUCCESS");
        msg.put("return_msg","OK");
        return msg;
    }

    @GetMapping("/state/{id}")
    public ResponseEntity<Integer> queryOrderStatus(@PathVariable("id")Long orderId){
        /**
         * @Description //TODO 查看订单支付状态
         * @Param [orderId]
         * @return org.springframework.http.ResponseEntity<java.lang.Integer>
         */
        return ResponseEntity.ok(orderService.queryPayStatus(orderId));
    }
}
